/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.util;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * 
 * @author 2600
 */
public final class DevideLogwithTime {

	private static String fromPath;
	private static ArrayList<String> products;
	private static String startTime;
	private static String endTime;
	private static String toPath;
	private static ArrayList<String> all;
	private static int originalSize;

	private DevideLogwithTime() {
		super();
	}

	/**
	 * @param args parameter
	 * @throws IOException exception
	 */
	public static void main(String[] args) throws IOException {
		all = new ArrayList<String>();
		originalSize = 0;
		products = new ArrayList<String>();

		startTime = "201205000000";
		endTime = "201305100000";
		// products.add("other");
		fromPath = "E:/盘古日志0419/2012.6/";
		// String fromPath =
		// "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-20120215-16/duration=2012021508/pagesearch_2012021508";
		// String fromPath =
		// "E:/pangu_new/2.pagesearch-file-20120215-16/pagesearch_20120215.log";
		// String fromPath =
		// "E:/pangu_new/1.pagesearch-ip-20120215-16/collect_temp/pagesearch/10.10.128.13/pagesearch_201202150000_0.log";

		File file = new File(fromPath);
		readFile(file);

		Collections.sort(all);

		toPath = "E:/盘古日志0419/vstar_sorted.log";
		// String toPath =
		// "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-
		//20120215-16/duration=2012021508/pagesearch_2012021508_new1.log";
		// String toPath =
		// "E:/pangu_new/2.pagesearch-file-20120215-16/pagesearch_20120215_New.log";
		// String toPath =
		// "E:/pangu_new/1.pagesearch-ip-20120215-16/collect_temp/pagesearch/10.10.128.13/pagesearch_201202150000_0_new.log";

		BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
				toPath)));
		int resultSize = 0;
		for (int i = 0; i < all.size(); i++) {
			if (all.get(i) != null) {
				writer.write(all.get(i) + "\n");
				resultSize++;
			}
		}
		writer.close();

		// System.out.println("originalSize:" + originalSize);
		// System.out.println("resultSize:" + resultSize);
	}

	/**
	 * reader for file/Directory
	 * 
	 * @param readfile
	 */
	private static void readFile(File readfile) throws
			IOException {
		if (!readfile.isDirectory()) {
			readFileContent(readfile);
		} else if (readfile.isDirectory()) {
			File[] fileList = readfile.listFiles();
			for (int i = 0; i < fileList.length; i++) {
				File editfile = fileList[i];

				if (!editfile.isDirectory()) {
					readFileContent(editfile);
				} else if (editfile.isDirectory()) {
					readFile(editfile);
				}
			}
		}
	}

	private static void readFileContent(File readfile)
			throws IOException {
		String fileName = readfile.getName();
		String[] fileDetail = fileName.split("_");
		String product = fileDetail[0];
		String time = fileDetail[1];
		if (time.compareTo(startTime) >= 0 && time.compareTo(endTime) < 0) { // in
																			// the
																			// timezone
			if (products.size() == 0) { // no product restrain
				readMatchFile(readfile);
			} else {
				for (int i = 0; i < products.size(); i++) {
					if (product.contains(products.get(i))) {
						readMatchFile(readfile);
					}

				}
			}
		}

	}

	private static void readMatchFile(File matchFile)
			throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(matchFile));
		String record = reader.readLine();
		while (record != null) {
			all.add(record);
			originalSize++;
			record = reader.readLine();
		}
		reader.close();
	}
}
